package com.airsystem.pos.bengkel.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.WindowConstants;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.interfaces.IMenu;
import com.airsystem.pos.bengkel.model.MenuModel;
import com.airsystem.pos.bengkel.view.ChangePassword;
import com.airsystem.pos.bengkel.view.Login;
import com.airsystem.pos.bengkel.view.MasterBilling;
import com.airsystem.pos.bengkel.view.MasterBillingDetail;
import com.airsystem.pos.bengkel.view.MasterCustomer;
import com.airsystem.pos.bengkel.view.MasterEmployee;
import com.airsystem.pos.bengkel.view.MasterServiceType;
import com.airsystem.pos.bengkel.view.MasterServices;
import com.airsystem.pos.bengkel.view.MasterSparepart;
import com.airsystem.pos.bengkel.view.MasterSparepartType;
import com.airsystem.pos.bengkel.view.MasterVehicle;
import com.airsystem.pos.bengkel.view.ServiceOrder;
import com.airsystem.pos.bengkel.view.SparepartOrder;
import com.airsystem.pos.bengkel.view.UserManagement;
import com.airsystem.pos.bengkel.view.report.BalanceSheetReport;
import com.airsystem.pos.bengkel.view.report.BillingReport;
import com.airsystem.pos.bengkel.view.report.CustomerBillsReport;
import com.airsystem.pos.bengkel.view.report.EmployeeReport;
import com.airsystem.pos.bengkel.view.report.SparepartBillsReport;

/**
 * @author Budi Oktaviyan <budi.oktaviyan@icloud.com>
 */
public class MenuController extends MenuModel implements IMenu {
	private static final Logger LOG = Logger.getLogger(MenuController.class.getSimpleName());

	private boolean isFormOpen, isSystemExit;

	private String setToday, setDate, setMonth, setYear, getDay, getDate, getHour, hours, minutes, seconds;

	private String[] month, days;

	private Calendar calendar;

	private Date date;

	private SimpleDateFormat dateFormat, formatDate, formatMonth, formatYear;

	private Timer workingTimer;

	private JInternalFrame[] internalFrame;

	public MenuController() {
		super();
	}

	public void setup(boolean authority) {
		itemUserManagement.setVisible(authority);
		itemChangePassword.setVisible(!authority);
		itemEmployee.setVisible(authority);
		itemBilling.setVisible(authority);
		itemBillingDetail.setVisible(authority);
		masterSeparator.setVisible(authority);
		itemEmployeeReport.setVisible(authority);
		itemBillingReport.setVisible(authority);
		reportSeparator.setVisible(authority);
	}

	public void timer() {
		date = new Date();
		dateFormat = new SimpleDateFormat("EEEE");
		setToday = dateFormat.format(date);

		month = new String[] { "Januari", "Februari", "Maret", "April", "Mei", "Juni",
							   "Juli", "Agustus", "September", "Oktober", "November", "Desember" };
		days = new String[] { "Senin", "Selasa", "Rabu", "Kamis", "Jum'at", "Sabtu", "Minggu" };

		formatDate = new SimpleDateFormat("dd");
		formatMonth = new SimpleDateFormat("MM");
		formatYear = new SimpleDateFormat("yyyy");

		setDate = formatDate.format(date);
		setMonth = formatMonth.format(date);
		setYear = formatYear.format(date);

		int i = 0;
		if (setToday.equals("Monday")) {
			i = 0;
		} else if (setToday.equals("Tuesday")) {
			i = 1;
		} else if (setToday.equals("Wednesday")) {
			i = 2;
		} else if (setToday.equals("Thursday")) {
			i = 3;
		} else if (setToday.equals("Friday")) {
			i = 4;
		} else if (setToday.equals("Saturday")) {
			i = 5;
		} else if (setToday.equals("Sunday")) {
			i = 6;
		}

		getDay = days[i];
		getDate = new StringBuilder(setDate).append(" ").append(month[Integer.parseInt(setMonth) - 1]).append(" ").append(setYear).toString();

		workingTimer = new Timer(1000, new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				calendar = Calendar.getInstance();

				int h = calendar.get(Calendar.HOUR_OF_DAY);
				int m = calendar.get(Calendar.MINUTE);
				int s = calendar.get(Calendar.SECOND);

				if (h <= 9) {
					hours = new StringBuilder("0").append(h).toString();
				} else {
					hours = new StringBuilder("").append(h).toString();
				}
				if (m <= 9) {
					minutes = new StringBuilder("0").append(m).toString();
				} else {
					minutes = new StringBuilder("").append(m).toString();
				}
				if (s <= 9) {
					seconds = new StringBuilder("0").append(s).toString();
				} else {
					seconds = new StringBuilder("").append(s).toString();
				}

				getHour = new StringBuilder(hours).append(":").append(minutes).append(":").append(seconds).toString();
				timeLabel.setText(new StringBuilder(getDay).append(", ").append(getDate).append(" | ").append(getHour).toString());
			}
		});

		workingTimer.start();
	}

	public void logout() {
		isSystemExit = false;
		checkFormOpen();

		if (!isFormOpen && !isSystemExit) {
			Login userLogin = new Login();
			userLogin.setVisible(true);
		}
	}

	public void exit() {
		if (askExit()) {
			checkFormOpen();
		} else {
			this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		}
	}

	public void openUserManagement() {
		if (!desktopPane.isAncestorOf(formUserManagement)) {
			formUserManagement = new UserManagement(this);
			desktopPane.add(formUserManagement);

			try {
				formUserManagement.setVisible(true);
				formUserManagement.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formUserManagement.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openChangePassword() {
		if (!desktopPane.isAncestorOf(formChangePassword)) {
			formChangePassword = new ChangePassword(this);
			desktopPane.add(formChangePassword);

			try {
				formChangePassword.setVisible(true);
				formChangePassword.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formChangePassword.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openMasterEmployee() {
		if (!desktopPane.isAncestorOf(formMasterEmployee)) {
			formMasterEmployee = new MasterEmployee(this);
			desktopPane.add(formMasterEmployee);

			try {
				formMasterEmployee.setVisible(true);
				formMasterEmployee.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formMasterEmployee.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openMasterBilling() {
		if (!desktopPane.isAncestorOf(formMasterBilling)) {
			formMasterBilling = new MasterBilling(this);
			desktopPane.add(formMasterBilling);

			try {
				formMasterBilling.setVisible(true);
				formMasterBilling.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formMasterBilling.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openMasterBillingDetail() {
		if (!desktopPane.isAncestorOf(formMasterBillingDetail)) {
			formMasterBillingDetail = new MasterBillingDetail(this);
			desktopPane.add(formMasterBillingDetail);

			try {
				formMasterBillingDetail.setVisible(true);
				formMasterBillingDetail.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formMasterBilling.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openMasterVehicle() {
		if (!desktopPane.isAncestorOf(formMasterVehicle)) {
			formMasterVehicle = new MasterVehicle(this);
			desktopPane.add(formMasterVehicle);

			try {
				formMasterVehicle.setVisible(true);
				formMasterVehicle.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formMasterVehicle.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openMasterCustomer() {
		if (!desktopPane.isAncestorOf(formMasterCustomer)) {
			formMasterCustomer = new MasterCustomer(this);
			desktopPane.add(formMasterCustomer);

			try {
				formMasterCustomer.setVisible(true);
				formMasterCustomer.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formMasterCustomer.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openMasterSparepartType() {
		if (!desktopPane.isAncestorOf(formMasterSparepartType)) {
			formMasterSparepartType = new MasterSparepartType(this);
			desktopPane.add(formMasterSparepartType);

			try {
				formMasterSparepartType.setVisible(true);
				formMasterSparepartType.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formMasterSparepartType.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openMasterSparepart() {
		if (!desktopPane.isAncestorOf(formMasterSparepart)) {
			formMasterSparepart = new MasterSparepart(this);
			desktopPane.add(formMasterSparepart);

			try {
				formMasterSparepart.setVisible(true);
				formMasterSparepart.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formMasterSparepart.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openMasterJasaType() {
		if (!desktopPane.isAncestorOf(formMasterJasaType)) {
			formMasterJasaType = new MasterServiceType(this);
			desktopPane.add(formMasterJasaType);

			try {
				formMasterJasaType.setVisible(true);
				formMasterJasaType.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formMasterJasaType.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openMasterJasa() {
		if (!desktopPane.isAncestorOf(formMasterJasa)) {
			formMasterJasa = new MasterServices(this);
			desktopPane.add(formMasterJasa);

			try {
				formMasterJasa.setVisible(true);
				formMasterJasa.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formMasterJasa.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openSparepartOrder() {
		if (!desktopPane.isAncestorOf(formSparepartOrder)) {
			formSparepartOrder = new SparepartOrder(this);
			desktopPane.add(formSparepartOrder);

			try {
				formSparepartOrder.setMaximum(true);
				formSparepartOrder.setVisible(true);
				formSparepartOrder.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formSparepartOrder.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openJasaOrder() {
		if (!desktopPane.isAncestorOf(formJasaOrder)) {
			formJasaOrder = new ServiceOrder(this);
			desktopPane.add(formJasaOrder);

			try {
				formJasaOrder.setMaximum(true);
				formJasaOrder.setVisible(true);
				formJasaOrder.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formJasaOrder.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openEmployeeReport() {
		try {
			formEmployeeReport = new EmployeeReport(this);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void openBillingReport() {
		if (!desktopPane.isAncestorOf(formBillingReport)) {
			formBillingReport = new BillingReport(this);
			desktopPane.add(formBillingReport);

			try {
				formBillingReport.setVisible(true);
				formBillingReport.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formBillingReport.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openSparepartDebit() {
		if (!desktopPane.isAncestorOf(formSparepartDebit)) {
			formSparepartDebit = new SparepartBillsReport(this);
			desktopPane.add(formSparepartDebit);

			try {
				formSparepartDebit.setVisible(true);
				formSparepartDebit.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formSparepartDebit.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openCustomerCredit() {
		if (!desktopPane.isAncestorOf(formCustomerCredit)) {
			formCustomerCredit = new CustomerBillsReport(this);
			desktopPane.add(formCustomerCredit);

			try {
				formCustomerCredit.setVisible(true);
				formCustomerCredit.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formCustomerCredit.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void openBalanceSheet() {
		if (!desktopPane.isAncestorOf(formBalanceSheet)) {
			formBalanceSheet = new BalanceSheetReport(this);
			desktopPane.add(formBalanceSheet);

			try {
				formBalanceSheet.setVisible(true);
				formBalanceSheet.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			try {
				formBalanceSheet.setSelected(true);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MenuController.this, "Gagal buka form!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	private boolean askExit() {
		try {
			Object[] objects = { "Ya", "Tidak" };
			int ask = JOptionPane.showOptionDialog(MenuController.this, "Keluar dari aplikasi?", "Konfirmasi",
					  JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, objects, objects[0]);

			if (ask == 0) {
				return isSystemExit = true;
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}

		return false;
	}

	private void checkFormOpen() {
		internalFrame = desktopPane.getAllFrames();

		if (internalFrame.length > 0) {
			isFormOpen = true;

			JOptionPane.showMessageDialog(MenuController.this, "Form masih terbuka!", "Pesan", JOptionPane.ERROR_MESSAGE);
			this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		} else if (internalFrame.length == 0) {
			isFormOpen = false;

			if (isSystemExit) {
				System.exit(0);
			} else {
				dispose();
			}
		}
	}
}